require "app_config"

class Iteration0 < ActiveRecord::Migration
  def self.up
    admin = UserType::ADMINISTRATOR
    author = UserType::AUTHOR
    teacher = UserType::TEACHER
    student = UserType::STUDENT

    query = <<-_SQL
      CREATE TABLE `user_types` (
        `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
        `name` VARCHAR( 100 ) NOT NULL ,
        UNIQUE (
          `name`
        )
      ) ENGINE = MYISAM;

      INSERT INTO `user_types` ( `id` , `name` )
        VALUES (
          '#{admin[:id]}', '#{admin[:name]}'
        ), (
          '#{author[:id]}', '#{author[:name]}'
        ), (
          '#{teacher[:id]}', '#{teacher[:name]}'
        ), (
          '#{student[:id]}', '#{student[:name]}'
        );

      CREATE TABLE `users` (
        `id` int(10) unsigned NOT NULL auto_increment,
        `login` varchar(100) NOT NULL,
        `password` varchar(100) NOT NULL,
        `email` varchar(100) NOT NULL,
        `user_type` int(10) unsigned NOT NULL,
        PRIMARY KEY  (`id`),
        UNIQUE KEY `login` (`login`)
      );
      
      INSERT INTO `users` ( `id` , `login` , `password` , `email`, `user_type` )
        VALUES (
        '1', '#{SUPERADMIN_LOGIN}', '#{User.encrypt(SUPERADMIN_PASSWORD)}', '#{SUPERADMIN_EMAIL}', '1'
      )
    _SQL

    query.split(";").each {|i|
      execute i
    }
  end

  def self.down
    drop_table :user_types
    drop_table :users
  end
end
